package cn.jiyun.classes.service.impl;

import cn.jiyun.classes.dao.ClassesDao;
import cn.jiyun.classes.dao.StudentDao;
import cn.jiyun.classes.dao.TeacherDao;
import cn.jiyun.classes.figen.StudentFigen;
import cn.jiyun.classes.figen.TeacherFigen;
import cn.jiyun.classes.pojo.*;
import cn.jiyun.classes.service.ClassesService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
@Slf4j
public class ClassesServiceImpl implements ClassesService {
    @Autowired
    private ClassesDao classesDao;
    @Autowired
    private StudentDao studentDao;
    @Autowired
    private TeacherDao teacherDao;
    @Autowired
    private StudentFigen studentFigen;
    @Autowired
    private TeacherFigen teacherFigen;

    @Override
    public Page findPage(ClassesDto classesDto) {
        Page<Classes> page1 = new Page<>(classesDto.getPage(), classesDto.getPageSize());
        LambdaQueryWrapper<Classes> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(Classes::getName,classesDto.getName());
        /*queryWrapper.eq(Classes::getCode,classesDto.getCode());*/
        Page<Classes> selectPage = classesDao.selectPage(page1, queryWrapper);
        return selectPage;
    }

    @Override
    public AdchannelVo delete(Integer id) {
        classesDao.deleteById(id);
        AdchannelVo adchannelVo = new AdchannelVo();
        adchannelVo.setCode(2000);
        adchannelVo.setMsg("删除成功");
        return adchannelVo;
    }

    @Override
    public AdchannelVo deletebyis(Integer[] ids) {
        for (Integer id : ids) {
            classesDao.deleteById(id);
        }
        AdchannelVo adchannelVo = new AdchannelVo();
        adchannelVo.setCode(2000);
        adchannelVo.setMsg("删除成功");
        return adchannelVo;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AdchannelVo add(Dto dto) {
        //班级表添加
        log.info("dto:{}",dto);
        Classes classes = dto.getClasses();
        int i = classesDao.insert(classes);
        //老师添加
        Teacher teacher = dto.getTeacher();
        teacherFigen.add(teacher);
        //学生表添加
       /* Student student = new Student();
        List<Student> studentList = dto.getStudentList();
        for (Student student1 : studentList) {
            BeanUtils.copyProperties(student1,student);
            studentDao.insert(student);
        }*/
        List<Student> studentList = dto.getStudentList();
        log.info("studentList:{}",studentList);
        for (Student student : studentList) {
            studentFigen.add(student);
        }

        AdchannelVo adchannelVo = new AdchannelVo();
        adchannelVo.setCode(2000);
        adchannelVo.setMsg("添加成功");
        return adchannelVo;
    }

    @Override
    public Dto findById(Integer id) {
        Classes classes = classesDao.selectById(id);
        List<Student> studentList = studentFigen.findById(id);
        Teacher teacher = teacherFigen.findById(id);
        Dto dto = new Dto();
        dto.setStudentList(studentList);
        dto.setTeacher(teacher);
        dto.setClasses(classes);
        return dto;
    }

    @Override
    public AdchannelVo edit(Dto dto) {
        //班级表修改
        log.info("dto:{}",dto);
        Classes classes = dto.getClasses();
        classesDao.updateById(classes);
        //老师修改
        Teacher teacher = dto.getTeacher();
        teacherFigen.edit(teacher);
        //先删除学生表的信息
        Integer cid = classes.getId();
        studentFigen.delByCid(cid);
        //添加学生信息
        List<Student> studentList = dto.getStudentList();
        for (Student student : studentList) {
            student.setCid(cid);
            studentFigen.add(student);
        }
        AdchannelVo adchannelVo = new AdchannelVo();
        adchannelVo.setCode(2000);
        adchannelVo.setMsg("修改成功");
        return adchannelVo;
    }
}
